﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Linq;
using Citi.GocAttributes.Domain;
using Citi.GocAttributes.WebUI.Models;

namespace Citi.GocAttributes.WebUI.Controllers
{
    public class TypeRuleResultController : ControllerBase<TypeRuleResultModel, TypeRuleResult>
    {
        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
        public ActionResult Index(DatabaseEnums.TypeResultType type)
        {
            var models = GetModelsByType(type);

            return View(models);
        }
        
        private IEnumerable<TypeRuleResultModel> GetModelsByType(DatabaseEnums.TypeResultType type)
        {
            return new TypeRuleResultModel().CreateModel(new TypeRuleResult().SelectActiveByType(type));
        }

        public ActionResult Delete(int id, DatabaseEnums.TypeResultType type)
        {
            DeleteBase(id);

            return PartialView("List", GetModelsByType(type));
        }

        [HttpGet]
        [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
        public ActionResult Form(DatabaseEnums.TypeResultType type, int? id)
        {
            return !id.HasValue
                ? PartialView(new TypeRuleResultModel { Type = type })
                : ActionBase(id, true);
        }

        [HttpPost]
        public ActionResult Form(TypeRuleResultModel model)
        {
            TypeRuleResult domain;

            if (model.Id == 0)
            {
                domain = new TypeRuleResult
                    {
                        Active = true,
                        LastUpdate = DateTime.Now,
                        ReferenceDate = DateTime.Today,
                        Type = (short)model.Type,
                        Value = model.Value
                    };
                domain.Save(domain);
                //return PartialView(new TypeRuleResultModel {Type = model.Type});
                return PartialView("List", GetModelsByType(model.Type));
            }

            domain = new TypeRuleResult().SelectById<TypeRuleResult>(model.Id);

            if (domain.RuleResults.Count != 0)
            {
                domain = domain.CopyEntity(domain);
                domain.ReferenceDate = DateTime.Today;
            }

            domain.LastUpdate = DateTime.Now;
            domain.Value = model.Value;

            domain.Save(domain);

            return PartialView("List", GetModelsByType(model.Type));
        }

        [HttpGet]
        public JsonResult GetAutocomplete(string searchTerm)
        {
            var functions = new TypeRuleResult().SelectAllActive<TypeRuleResult>()
                            //.Where(x => x.Type == (int)DatabaseEnums.TypeResultType.DeckRegional1 && x.Value.ToUpper().Contains(searchTerm.ToUpper()))
                            .Where(x => x.Type == (int)DatabaseEnums.TypeResultType.OTFunctionL4 && x.Value.ToUpper().Contains(searchTerm.ToUpper()))
                            .Select(c => new { Description = c.Value, c.Id });

            return Json(functions.Select(x => new
            {
                x.Id,
                x.Description
            }), JsonRequestBehavior.AllowGet);
        }
    }
}
